package com.eorchis.module.trainingplan.service.impl;

import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import com.eorchis.constants.DeptConstants;
import com.eorchis.constants.RoleConstants;
import com.eorchis.core.basedao.entity.IBaseEntity;
import com.eorchis.core.basedao.dao.IDaoSupport;
import com.eorchis.core.service.impl.AbstractBaseService;
import com.eorchis.core.springext.mvc.SessionConstant;
import com.eorchis.core.annotation.AuditModule;
import com.eorchis.module.classplanapply.ui.commond.ClassPlanApplyQueryCommond;
import com.eorchis.module.department.domain.Department;
import com.eorchis.module.role.domain.Role;
import com.eorchis.module.trainingplan.dao.ITrainingPlanDao;
import com.eorchis.module.trainingplan.domain.ClassPlanApplyQueryBean;
import com.eorchis.module.trainingplan.domain.TrainingPlan;
import com.eorchis.module.trainingplan.domain.TrainingPlanApplyContrast;
import com.eorchis.module.trainingplan.domain.TrainingPlanBean;
import com.eorchis.module.trainingplan.domain.yearlyPlanExport;
import com.eorchis.module.trainingplan.service.ITrainingPlanService;
import com.eorchis.module.trainingplan.ui.commond.TrainingPlanFileValidCommond;
import com.eorchis.module.trainingplan.ui.commond.TrainingPlanFileZZBValidCommond;
import com.eorchis.module.trainingplan.ui.commond.TrainingPlanQueryCommond;
import com.eorchis.module.trainingplan.ui.commond.TrainingPlanValidCommond;
import com.eorchis.module.util.StringUtil;
import com.eorchis.util.excel.ExcelImportResult;
import com.eorchis.util.excel.ExcelUtil;
import com.eorchis.utils.utils.PropertyUtil;

/**
 * Title: TrainingPlanServiceImpl<br>
 * Description: 培训计划管理业务接口实现<br>
 * Company: GoldGov<br>
 * Copyright @ 2012 GoldGov .All rights reserved.<br>
 * @author RongSL
 * @createDate 2016-07-12
 * @version $Revision$
 */
// 声明本类为Server实现类，声明bean名称
@Service(value = "com.eorchis.module.trainingplan.service.impl.TrainingPlanServiceImpl")
@AuditModule("培训计划")
public class TrainingPlanServiceImpl 
			extends AbstractBaseService 
		implements ITrainingPlanService {
		
	@Autowired
	@Qualifier("com.eorchis.module.trainingplan.dao.impl.TrainingPlanDaoImpl")
	private ITrainingPlanDao trainingPlanDao;
		

	public IDaoSupport getDaoSupport() {
		return trainingPlanDao;
	}
	@SuppressWarnings("unchecked")
	public TrainingPlanValidCommond toCommond(IBaseEntity entity) {
		return new TrainingPlanValidCommond((TrainingPlan)entity);
	}

	public TrainingPlanValidCommond findTrainingPlan(TrainingPlanValidCommond vCommond){
		return trainingPlanDao.findTrainingPlan(vCommond);
	}
	
	public void updateTrainingPlanValid(TrainingPlanValidCommond vCommond){
		TrainingPlan tp=new TrainingPlan();
		BeanUtils.copyProperties(vCommond, tp);
		trainingPlanDao.update(tp);
	}
	
	/**
	 * 查看培训计划归档
	 * @author RongSL
	 * @Date 2016-07-13
	 * */
	public TrainingPlanFileValidCommond findTrainingPlan(TrainingPlanFileValidCommond vCommond){
		return trainingPlanDao.findTrainingPlan(vCommond);
	}

	/**
	 * 查看培训计划中组部归档
	 * 
	 * @author RongSL
	 * @Date 2016-07-13
	 * */
	@Override
	public TrainingPlanFileZZBValidCommond findTrainingPlan(TrainingPlanFileZZBValidCommond vCommond){
		return trainingPlanDao.findTrainingPlan(vCommond);
	}
	
	/**
	 * 判断计划是否可以撤回
	 * @author RongSL
	 * @Date 2016-07-13
	 * */
	public Boolean judgePlanWithdraw(TrainingPlanQueryCommond queryCommond){
		return trainingPlanDao.judgePlanWithdraw(queryCommond);
	}
	
	/**
	 * 判断计划是否可以删除
	 * @author RongSL
	 * @Date 2016-07-13
	 * */
	@Override
	public Boolean judgePlanDelete(TrainingPlanQueryCommond queryCommond){
		return trainingPlanDao.judgePlanDelete(queryCommond);
	}
	
	/**
	 * 判断计划是否可以驳回
	 * @author RongSL
	 * @Date 2016-07-13
	 * */
	@Override
	public Boolean judgePlanReject(TrainingPlanQueryCommond queryCommond){
		return trainingPlanDao.judgePlanReject(queryCommond);
	}
	
	/**
	 * @category 获取导入结果
	 * @param is
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ExcelImportResult divideImportExcelPlan(InputStream is, HttpServletRequest request) throws Exception{
		ExcelImportResult result = new ExcelImportResult();
		result.setResultSign(true);
		StringBuffer resultString = new StringBuffer("");
		Workbook wb = ExcelUtil.createWorkbook(is);
		// 只解析第一个工作表中的内容
		Sheet sheet = wb.getSheetAt(0);
		// 最大行号
		int lastRowNum = sheet.getLastRowNum();
		int errorLine = 1;

		// 获取第一行 培训班编号
		Row firstRow = sheet.getRow(0);
		// 计算培训计划对应列号
		Iterator<Cell> classIterator = firstRow.cellIterator();
		while (classIterator.hasNext()) {
			Cell next = classIterator.next();
			// 列号
			int columnIndex = next.getColumnIndex();
			if (columnIndex == 0) {
				continue;
			}
			//获取培训计划编号
			String applyNum = ExcelUtil.getCellValue(next);
			if(PropertyUtil.objectNotEmpty(applyNum)){//计划编码不为空
				Role currentRole = (Role)request.getSession().getAttribute(SessionConstant.CURRENT_ROLE);
//				request.getSession().getAttributeNames()
//				Department dep=(Department)request.getSession().getAttribute("currentScope");
				Department currentScope = (Department) request.getSession().getAttribute(SessionConstant.CURRENT_SCOPE);
				if(RoleConstants.ROLE_CODE_BW_ADMIN.equals(currentRole.getRoleCode())){//部委管理员
					
					TrainingPlanValidCommond tp=new TrainingPlanValidCommond();
					tp.setApplyNum(applyNum);
					if(!TrainingPlanSearch(tp)){
						resultString.append(errorLine+++"、导入excel中第"+(columnIndex+1)+"列编号为"+applyNum+"的培训计划不存在，无法导入名额。\r\n");
						result.setResultSign(false);
					}else{
						TrainingPlanQueryCommond queryCommond = new TrainingPlanQueryCommond();
						queryCommond.setSearchApplyNum(applyNum.trim());
						queryCommond.setSearchUndertakeID(currentScope.getDeptID().trim());
						List<?> planList = findList(queryCommond);
						if(!PropertyUtil.objectNotEmpty(planList)){
							resultString.append(errorLine+++"、导入excel中第"+(columnIndex+1)+"列编号为"+applyNum+"的培训计划非本部委创建，无法导入名额。\r\n");
							result.setResultSign(false);
						}
					}
					//验证是否已发布
					TrainingPlanQueryCommond tpq = new TrainingPlanQueryCommond();
					tpq.setSearchApplyNum(applyNum);
					List<TrainingPlanValidCommond> li=(List<TrainingPlanValidCommond>)findList(tpq);
					if(PropertyUtil.objectNotEmpty(li)){
						TrainingPlanValidCommond tpv=li.get(0);
						if(tpv.getPublishState().equals(TrainingPlan.PUBLISHSTATE_Y)){
							resultString.append("已经发布的培训计划不允许再导入名额。\r\n");
							result.setResultSign(false);
						}
					}
				}
			} else {
				resultString.append(errorLine+++"、导入excel中第1行第"+(columnIndex+1)+"列内容错误，错误信息：培训计划编号为空。\r\n");
				result.setResultSign(false);
			}
			break;
		}

		for (int i = 3; i < lastRowNum; i++) {
			Row row = sheet.getRow(i);//行号
			for (int j = 0; j < row.getLastCellNum()-1; j++) {
				Cell cell = row.getCell(j);
				String cellValue = ExcelUtil.getCellValue(cell);
				if(j==0){
					//报名单位简称是否正确
					boolean flag = DeptConstants.PROVINCE_SHORT_DEPNAME_MAPPING.containsValue(cellValue);
					if(!flag){
						resultString.append(errorLine+++"、导入excel中第"+(i+1)+"行第"+(j+1)+"列内容错误，错误信息：内容“"+cellValue+"”不是32个省份名称，建议模板第一列不要修改。\r\n");
						result.setResultSign(false);
					}
				}else{
					try {
						BigDecimal bd = !PropertyUtil.objectNotEmpty(cellValue.trim()) ? new BigDecimal(0) : new BigDecimal(cellValue.trim());
						if(bd.longValue()<0||bd.longValue()>1000){
							resultString.append(errorLine+++"、导入excel中第"+(i+1)+"行第"+(j+1)+"列内容错误，错误信息：内容“"+cellValue+"”必须为0~999间的正整数。\r\n");
							result.setResultSign(false);
						}
					} catch (Exception e) {
						resultString.append(errorLine+++"、导入excel中第"+(i+1)+"行第"+(j+1)+"列内容错误，错误信息：内容“"+cellValue+"”必须为0~999间的正整数。\r\n");
						result.setResultSign(false);
					}
				}
			}
		}
		result.setErrorMessage(resultString);
		return result;
	}
	
	/**
	 * @category 获取导入结果
	 * @param is
	 * @return
	 * @throws Exception
	 */
	public ExcelImportResult divideImportExcelPlanSheet(Sheet sheet, String applyNum,HttpServletRequest request){
		ExcelImportResult result = new ExcelImportResult();
		result.setResultSign(true);
		StringBuffer resultString = new StringBuffer();
		// 最大行号
		int lastRowNum = sheet.getLastRowNum();
		int errorLine = 1;
		
		// 获取第一行 培训班编号
//		Row firstRow = sheet.getRow(0);
		// 计算培训计划对应列号
//		Iterator<Cell> classIterator = firstRow.cellIterator();
//		while (classIterator.hasNext()) {
//			Cell next = classIterator.next();
//			// 列号
//			int columnIndex = next.getColumnIndex();
//			if (columnIndex == 0 || ((columnIndex & 1) == 0)) {
//				continue;
//			}
			if(PropertyUtil.objectNotEmpty(applyNum)){//计划编码不为空
				Role currentRole = (Role)request.getSession().getAttribute(SessionConstant.CURRENT_ROLE);
//				request.getSession().getAttributeNames()
//				Department dep=(Department)request.getSession().getAttribute("currentScope");
				Department currentScope = (Department) request.getSession().getAttribute(SessionConstant.CURRENT_SCOPE);
				if(RoleConstants.ROLE_CODE_BW_ADMIN.equals(currentRole.getRoleCode())&&!currentScope.getDeptID().equals("1")){//部委管理员
					
					TrainingPlanValidCommond tp=new TrainingPlanValidCommond();
					tp.setApplyNum(applyNum);
					if(!TrainingPlanSearch(tp)){
						resultString.append(errorLine+++"、导入excel中编号为"+applyNum+"的培训计划不存在，无法导入名额。\r\n");
						result.setResultSign(false);
					}else{
						TrainingPlanQueryCommond queryCommond = new TrainingPlanQueryCommond();
						queryCommond.setSearchApplyNum(applyNum.trim());
						queryCommond.setSearchUndertakeID(currentScope.getDeptID().trim());
						List<?> planList = findList(queryCommond);
						if(!PropertyUtil.objectNotEmpty(planList)){
							resultString.append(errorLine+++"、导入excel中编号为"+applyNum+"的培训计划非本部委创建，无法导入名额。\r\n");
							result.setResultSign(false);
						}
					}
				}
			} else {
				resultString.append(errorLine+++"、导入excel内容错误，错误信息：培训计划编号为空。\r\n");
				result.setResultSign(false);
			}
//		}
		
		for (int i = 3; i < lastRowNum; i++) {
			Row row = sheet.getRow(i);//行号
			for (int j = 0; j < row.getLastCellNum(); j++) {
				Cell cell = row.getCell(j);
				String cellValue = ExcelUtil.getCellValue(cell);
				if(j==0){
					//报名单位简称是否正确
					boolean flag = DeptConstants.PROVINCE_SHORT_DEPNAME_MAPPING.containsValue(cellValue);
					if(!flag){
						resultString.append(errorLine+++"、导入excel中第"+(i+1)+"行第"+(j+1)+"列内容错误，错误信息：内容“"+cellValue+"”不是32个省份名称，建议模板第一列不要修改。\r\n");
						result.setResultSign(false);
					}
				}else{
					try {
						BigDecimal bd = !PropertyUtil.objectNotEmpty(cellValue.trim()) ? new BigDecimal(0) : new BigDecimal(cellValue.trim());
						if(bd.longValue()<0||bd.longValue()>1000){
							resultString.append(errorLine+++"、导入excel中第"+(i+1)+"行第"+(j+1)+"列内容错误，错误信息：内容“"+cellValue+"”必须为0~999间的正整数。\r\n");
							result.setResultSign(false);
						}
					} catch (Exception e) {
						resultString.append(errorLine+++"、导入excel中第"+(i+1)+"行第"+(j+1)+"列内容错误，错误信息：内容“"+cellValue+"”必须为0~999间的正整数。\r\n");
						result.setResultSign(false);
					}
				}
			}
		}
		result.setErrorMessage(resultString);
		return result;
	}
	
	/**
	 * 更新计划归档表
	 * @author RongSL
	 * @Date 2016-07-13
	 * */
	@Override
	public void updatePlanFile(TrainingPlanFileValidCommond ValidCommond,String array){
		trainingPlanDao.updatePlanFile(ValidCommond, array);
	}
	
	/**
	 * 更新计划归档表
	 * @author RongSL
	 * @Date 2016-07-13
	 * */
	@Override
	public void updatePlanFile(TrainingPlanFileZZBValidCommond ValidCommond,String array){
		trainingPlanDao.updatePlanFile(ValidCommond, array);
	}
	
	/**
	 * 塞值
	 * @Date 2016-7-19
	 * @author RongSL
	 * */
	@Override
	public TrainingPlanBean ValidCommondToTrainingPlanBean(TrainingPlanValidCommond tpv,TrainingPlanFileValidCommond tpf){
		TrainingPlanBean tpb=new TrainingPlanBean();
		if(PropertyUtil.objectNotEmpty(tpf)){
			//Old
			tpb.setApplyNumOld(tpf.getApplyNum());
			tpb.setClassNameOld(tpf.getClassName());
			tpb.setClassObjectiveOld(tpf.getClassObjective());
			tpb.setClassValuesOld(tpf.getClassValues());
			tpb.setLinkManOld(tpf.getLinkMan());
			tpb.setLinkModeOld(tpf.getLinkMode());
			tpb.setFaxOld(tpf.getFax());
			tpb.setPlanDayOld(tpf.getPlanDay());
//			tpb.setPlanDateOld(tpf.getPlanDate());
			tpb.setStartPlanDateOldStr(tpf.getStartPlanDateStr());
			tpb.setEndPlanDateOldStr(tpf.getEndPlanDateStr());
			tpb.setPlanIdOld(tpf.getPlanId());
			tpb.setTrainingAddressOld(tpf.getTrainingAddress());
			tpb.setTrainingObjectOld(tpf.getTrainingObject());
			tpb.setJointNameOld(tpf.getJointName());
			tpb.setUndertakeNameOld(tpf.getUndertakeName());
			tpb.setJointIdOld(tpf.getJointId());
			tpb.setUndertakeIdOld(tpf.getUndertakeId());
			//State
			tpb.setApplyNumState(StringUtil.compareString(tpv.getApplyNum(),tpf.getApplyNum()));
			tpb.setClassNameState(StringUtil.compareString(tpv.getClassName(),tpf.getClassName()));
			tpb.setClassObjectiveState(StringUtil.compareString(tpv.getClassObjective(),tpf.getClassObjective()));
			tpb.setClassValuesState(StringUtil.compareString(tpv.getClassValues(),tpf.getClassValues()));
			tpb.setLinkManState(StringUtil.compareString(tpv.getLinkMan(),tpf.getLinkMan()));
			tpb.setLinkModeState(StringUtil.compareString(tpv.getLinkMode(),tpf.getLinkMode()));
			tpb.setFaxState(StringUtil.compareString(tpv.getFax(),tpf.getFax()));
			tpb.setPlanDayState(StringUtil.compareString(tpv.getPlanDay()+"",tpf.getPlanDay()+""));
			tpb.setTrainingAddressState(StringUtil.compareString(tpv.getTrainingAddress(),tpf.getTrainingAddress()));
			tpb.setTrainingObjectState(StringUtil.compareString(tpv.getTrainingObject(),tpf.getTrainingObject()));
			tpb.setJointNameState(StringUtil.compareString(tpv.getJointName(), tpf.getJointName()));
			tpb.setUndertakeNameState(StringUtil.compareString(tpv.getUndertakeName(),tpf.getUndertakeName()));
			tpb.setJointIdState(StringUtil.compareString(tpv.getJointId(), tpf.getJointId()));
			tpb.setUndertakeIdState(StringUtil.compareString(tpv.getUndertakeId(),tpf.getUndertakeId()));
//			tpb.setPlanDateState(StringUtil.compareString(tpv.getPlanDate(),tpf.getPlanDate()));
			tpb.setStartPlanDateStateStr(StringUtil.compareString(tpv.getStartPlanDateStr(),tpf.getStartPlanDateStr()));
			tpb.setEndPlanDateStateStr(StringUtil.compareString(tpv.getEndPlanDateStr(),tpf.getEndPlanDateStr()));
		}
		//New
		tpb.setApplyNumNew(tpv.getApplyNum());
		tpb.setClassNameNew(tpv.getClassName());
		tpb.setClassObjectiveNew(tpv.getClassObjective());
		tpb.setClassValuesNew(tpv.getClassValues());
		tpb.setLinkManNew(tpv.getLinkMan());
		tpb.setLinkModeNew(tpv.getLinkMode());
		tpb.setFaxNew(tpv.getFax());
		tpb.setPlanDayNew(tpv.getPlanDay());
		tpb.setPlanIdNew(tpv.getPlanId());
//		tpb.setPlanDateNew(tpv.getPlanDate());
		tpb.setStartPlanDateNewStr(tpv.getStartPlanDateStr());
		tpb.setEndPlanDateNewStr(tpv.getEndPlanDateStr());
		tpb.setTrainingAddressNew(tpv.getTrainingAddress());
		tpb.setTrainingObjectNew(tpv.getTrainingObject());
		tpb.setJointNameNew(tpv.getJointName());
		tpb.setUndertakeNameNew(tpv.getUndertakeName());
		tpb.setJointIdNew(tpv.getJointId());
		tpb.setUndertakeIdNew(tpv.getUndertakeId());
		
		return tpb;
	}
	
	/**
	 * 计划下发
	 * @author RongSL
	 * @Date 2016-07-21
	 * */
	@Override
	public void planIssuedAdd(TrainingPlanQueryCommond queryCommond){
		trainingPlanDao.planIssuedAdd(queryCommond);
	}

	/**
	 * 更新计划总人数
	 * @author RongSL
	 * @Date 2016-07-25
	 * */
	public void updatePlanNum(TrainingPlanQueryCommond queryCommond){
		trainingPlanDao.updatePlanNum(queryCommond);
	}
	
	/**
	 * 查看培训计划列表
	 * @Date 2016-07-25
	 * @author RongSL
	 * */
	public List<TrainingPlanValidCommond> findPlanList(TrainingPlanQueryCommond queryCommond){
		return trainingPlanDao.findPlanList(queryCommond);
	}
	
	/**
	 * 查看下发列表
	 * @Date 2016-07-25
	 * @author RongSL
	 * */
	public List<TrainingPlanValidCommond> lookIssuedList(TrainingPlanQueryCommond queryCommond){
		return trainingPlanDao.lookIssuedList(queryCommond);
	}
	
	/**
	 * 查看反馈意见列表
	 * @Date 2016-07-25
	 * @author RongSL
	 * @return 
	 * */
	public List<TrainingPlanValidCommond> findFeedbackList(TrainingPlanQueryCommond queryCommond){
		return trainingPlanDao.findFeedbackList(queryCommond);
	}
	
	/**
	 * 下发单位列表
	 * @Date 2016-07-25
	 * @author RongSL
	 * */
	public List<TrainingPlanValidCommond> findIssuedList(TrainingPlanQueryCommond queryCommond){
		return trainingPlanDao.findIssuedList(queryCommond);
	}
	
	public List<ClassPlanApplyQueryBean> findClassPlanApplyQueryBeanList(ClassPlanApplyQueryCommond queryCommond){
		return trainingPlanDao.findClassPlanApplyQueryBeanList(queryCommond);
	}
	/**
	 * 查询培训班计划名额(该地区所有已下发的计划)
	 * @author ZhouYG
	 * @throws Exception 
	 * @createDate 2016-09-20
	 */
	public List<ClassPlanApplyQueryBean> findClassPlanApplyList(TrainingPlanQueryCommond queryCommond) throws Exception{
		return trainingPlanDao.findClassPlanApplyList(queryCommond);
	}
	/**
	 * 塞值
	 * @Date 2016-7-19
	 * @author RongSL
	 * */
	@Override
	public List<TrainingPlanApplyContrast> classPlanApplyQueryBeanToTrainingPlanApplyContrast(List<ClassPlanApplyQueryBean> resultList1,List<ClassPlanApplyQueryBean> resultList2){
		List<TrainingPlanApplyContrast> list=new ArrayList<TrainingPlanApplyContrast>();
		TrainingPlanApplyContrast tp=null;
		TrainingPlanApplyContrast tpa=new TrainingPlanApplyContrast();
		Integer cityCount1=0;
		Integer CountyCount1=0;
		Integer cityCount2=0;
		Integer CountyCount2=0;
		if(PropertyUtil.objectNotEmpty(resultList2)){
			for(int i=0;i<resultList2.size();i++){
				
				cityCount1+=StringUtil.nullToNumber(resultList1.get(i).getCityPlanCount());
				CountyCount1+=StringUtil.nullToNumber(resultList1.get(i).getCountyPlanCount());
				cityCount2+=StringUtil.nullToNumber(resultList2.get(i).getCityPlanCount());
				CountyCount2+=StringUtil.nullToNumber(resultList2.get(i).getCountyPlanCount());

				tp=new TrainingPlanApplyContrast();
				tp.setObjectID(resultList1.get(i).getDeptId());
				tp.setContrastClassName(resultList1.get(i).getClassName());
				tp.setContrastObject(resultList1.get(i).getDeptCode());
				tp.setContrastCityValue(resultList1.get(i).getCityPlanCount());
				tp.setContrastCityState(StringUtil.compareInt(resultList1.get(i).getCityPlanCount(),resultList2.get(i).getCityPlanCount()));
				tp.setContrastCountyValue(resultList1.get(i).getCountyPlanCount());
				tp.setContrastCountyState(StringUtil.compareInt(resultList1.get(i).getCountyPlanCount(),resultList2.get(i).getCountyPlanCount()));
				tp.setContrastCountValue(resultList1.get(i).getPlanCount());
				tp.setContrastCountState(StringUtil.compareInt(resultList1.get(i).getPlanCount(),resultList2.get(i).getPlanCount()));
				list.add(tp);
			}

			tpa.setContrastClassName("合计");
			tpa.setContrastCityValue(cityCount1);
			tpa.setContrastCountyValue(CountyCount1);
			tpa.setContrastCountValue(cityCount1+CountyCount1);
			tpa.setContrastCityState(StringUtil.compareInt(cityCount1, cityCount2));
			tpa.setContrastCountyState(StringUtil.compareInt(CountyCount1, CountyCount2));
			tpa.setContrastCountState(StringUtil.compareInt(cityCount1+CountyCount1,cityCount2+CountyCount2));
			tpa.setContrastObject("合计");
			tpa.setObjectID(1+"");
		}else{
			for(ClassPlanApplyQueryBean c:resultList1){
				
				cityCount1+=StringUtil.nullToNumber(c.getCityPlanCount());
				CountyCount1+=StringUtil.nullToNumber(c.getCountyPlanCount());

				tp=new TrainingPlanApplyContrast();
				tp.setObjectID(c.getDeptId());
				tp.setContrastClassName(c.getClassName());
				tp.setContrastObject(c.getDeptCode());
				tp.setContrastCityValue(c.getCityPlanCount());
				tp.setContrastCityState(true);
				tp.setContrastCountyValue(c.getCountyPlanCount());
				tp.setContrastCountyState(true);
				tp.setContrastCountValue(c.getPlanCount());
				tp.setContrastCountState(true);
				list.add(tp);
			}
			
			tpa.setContrastClassName("合计");
			tpa.setContrastCityValue(cityCount1);
			tpa.setContrastCountyValue(CountyCount1);
			tpa.setContrastCountValue(cityCount1+CountyCount1);
			tpa.setContrastCityState(true);
			tpa.setContrastCountyState(true);
			tpa.setContrastCountState(true);
			tpa.setContrastObject("合计");
			tpa.setObjectID(1+"");
		}
		
		LinkedList<TrainingPlanApplyContrast> linkedList=new LinkedList<TrainingPlanApplyContrast>();
		linkedList.addAll(list);
		linkedList.addFirst(tpa);
		list.clear();
		list.addAll(linkedList);
		return list;
	}
	
	/**
	 * 变更培训计划状态
	 * @author RongSL
	 * @Date 2016-07-28
	 * */
	public void updatePlanState(TrainingPlanQueryCommond queryCommond){
		trainingPlanDao.updatePlanState(queryCommond);
	}
	
	/**
	 * 地方查看培训计划列表
	 * @Date 2016-07-25
	 * @author RongSL
	 * */
	public List<TrainingPlanValidCommond> findPlanListDF(TrainingPlanQueryCommond queryCommond) {
		return trainingPlanDao.findPlanListDF(queryCommond);
	}
	
	/**
	 * 地方查看培训计划列表
	 * @Date 2016-07-25
	 * @author RongSL
	 * */
	public List<TrainingPlanValidCommond> findPlanListBW(TrainingPlanQueryCommond queryCommond) {
		return trainingPlanDao.findPlanListBW(queryCommond);
	}
	
	/**
	 * 删除计划相关数据
	 * @Date 2016-07-31
	 * */
	public void deletePlanOther(TrainingPlanQueryCommond queryCommond){
		trainingPlanDao.deletePlanApply(queryCommond);
		trainingPlanDao.deletePlanOpinion(queryCommond);
		trainingPlanDao.deletePlanIssued(queryCommond);
		
		//清除所有归档
		trainingPlanDao.deletePlanFile(queryCommond);
		trainingPlanDao.deletePlanFileZZB(queryCommond);
	}
	
	/**
	 * 更新培训计划名额
	 * @Date 2016-08-01
	 * */
	public void updatePlanApply(String... id){
		trainingPlanDao.updatePlanApply(id);
	}
	
	/**
	 * 根据部门名称查找部门ID
	 * @Date 2016-08-08
	 * */
	public String findAdminDeptId(TrainingPlanQueryCommond queryCommond){
		return trainingPlanDao.findAdminDeptId(queryCommond);
	}
	
	/**
	 * 判断有无培训计划
	 * */
	public boolean TrainingPlanSearch(TrainingPlanValidCommond vCommond){
		return trainingPlanDao.TrainingPlanSearch(vCommond);
	}
	
	/**
	 * 获得培训计划最大编号
	 * */
	public String getMaxApplyNum(String deptCode,Integer year){
		return trainingPlanDao.getMaxApplyNum(deptCode,year);
	}
	
	/**
	 * 获得培训班最大编号
	 * */
	public String getMaxClassNum(String year){
		return trainingPlanDao.getMaxClassNum(year);
	}
	
	/**
	 * 根据计划id查找地方部门id
	 * @author RongSL
	 * @date 2016-08-15
	 * */
	public String[] findDeptId(TrainingPlanValidCommond commond){
		return trainingPlanDao.findDeptId(commond);
	}
	
	/**
	 * 获得中组部下发时附加消息
	 * @author RongSL
	 * @Date 2016-09-05
	 * */
	@Override
	public String getOpinion(TrainingPlanValidCommond commond){
		return trainingPlanDao.getOpinion(commond);
	}
	
	/**
	 * 年度培训计划名额分配表
	 * @author RongSL
	 * @Date 2016-09-24
	 * */
	@Override
	public List<yearlyPlanExport> ExportYearPlan(TrainingPlanQueryCommond queryCommond) throws Exception{
		return trainingPlanDao.ExportYearPlan(queryCommond);
	}
}
